package us.codecraft.webmagic.main;

import com.alibaba.fastjson.JSONObject;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.model.samples.FF14Wiki;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.processor.PageProcessor;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @description:
 * @author: dqt
 * @date: 2021/3/25 16:16
 */
public class Test implements PageProcessor {


        private Site site = Site.me().setDomain("ff14.huijiwiki.com");

        @Override
        public void process(Page page) {
            List<String> links = page.getHtml().links().all();
            page.addTargetRequests(links);
            page.putField("title", page.getHtml().xpath("//*[@id=\"content-actions\"]/ul/li[1]/a/text()").toString());
            // page.putField("content", page.getHtml().$("div.content").toString());
            page.putField("cardName",page.getHtml().xpath("//*[@id=\"mw-content-text\"]/div/div/div/div/table/tbody/tr/td[1]/a/text()").all());
            page.putField("icon",page.getHtml().xpath("//*[@id=\"mw-content-text\"]/div/div/div/div/table/tbody/tr/td[2]/img/@src").all());
            page.putField("cardUrl",page.getHtml().xpath("//*[@id=\"mw-content-text\"]/div/div/div/div/table/tbody/tr/td[3]/img/@src").all());
            page.putField("starLevel",page.getHtml().xpath("//*[@id=\"mw-content-text\"]/div/div/div/div/table/tbody/tr/td[4]/text()").all());
            page.putField("cardType",page.getHtml().xpath("//*[@id=\"mw-content-text\"]/div/div/div/div/table/tbody/tr/td[5]/text()").all());
            page.putField("numUp",page.getHtml().xpath("//*[@id=\"mw-content-text\"]/div/div/div/div/table/tbody/tr/td[6]/text()").all());
            page.putField("numRight",page.getHtml().xpath("//*[@id=\"mw-content-text\"]/div/div/div/div/table/tbody/tr/td[7]/text()").all());
            page.putField("numDown",page.getHtml().xpath("//*[@id=\"mw-content-text\"]/div/div/div/div/table/tbody/tr/td[8]/text()").all());
            page.putField("numLeft",page.getHtml().xpath("//*[@id=\"mw-content-text\"]/div/div/div/div/table/tbody/tr/td[9]/text()").all());


            ResultItems resultItems = page.getResultItems();

            List<String> cardName = resultItems.get("cardName");
            List<String> icon = resultItems.get("icon");
            List<String> cardUrl = resultItems.get("cardUrl");
            List<Integer> starLevel = resultItems.get("starLevel");
            List<String> cardType = resultItems.get("cardType");
            List<Integer> numUp = resultItems.get("numUp");
            List<Integer> numRight = resultItems.get("numRight");
            List<Integer> numDown = resultItems.get("numDown");
            List<Integer> numLeft = resultItems.get("numLeft");

            List<Map>list=new ArrayList<Map>();


            for (int i=0;i<cardName.size();i++){

                Map<String,Object>map=new ConcurrentHashMap<String, Object>();
                map.put("cardName",cardName.get(i));
                map.put("icon",icon.get(i));
                map.put("cardUrl",cardUrl.get(i));
                map.put("starLevel",starLevel.get(i));
                map.put("cardType",cardType.get(i));
                map.put("numUp",numUp.get(i));
                map.put("numRight",numRight.get(i));
                map.put("numDown",numDown.get(i));
                map.put("numLeft",numLeft.get(i));
                list.add(map);

            }




            // System.out.println(list.toString());

            String s = JSONObject.toJSONString(list);
            System.out.println(s);
            System.exit(1);


        }

        @Override
        public Site getSite() {
            return site;

        }

        public static void main(String[] args) {
            Spider.create(new Test()).addUrl("https://ff14.huijiwiki.com/wiki/%E4%B9%9D%E5%AE%AB%E5%B9%BB%E5%8D%A1%E5%8D%A1%E7%89%8C")
                    .addPipeline(new ConsolePipeline()).run();
        }


}
